class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        struct ListNode* prev = head;
        struct ListNode* target = head;
        while(target != NULL)
        {
            if(head == NULL)
            {
                return head;
            }
            else if(head->val == val)
            {
                head = head->next;
                prev = head;
                target = head;
            }
            else if(target->val == val)
            {
                prev->next = target->next;
                target = prev->next;
            }
            else if(target != NULL && target-> val != val)
            {
                prev = target;
                target = target->next;
            }
        }
        return head;
    }
};